package wviz.index;


/**

 * this data structure stores 2 words with there connectivity link which can be
 * for example there coocurrance or something similar; this connectivity coocurrance
 * is uesd to make the visual summerizing graph...
 */
public class TwoWords implements Comparable {

    protected double connectivity;
    private String word1, word2;

    public static final double NO_CONNECTIVITY = 0; //Double.NEGATIVE_INFINITY;

    public TwoWords(String t1, String t2) {
            this(t1, t2, NO_CONNECTIVITY);
        }

    public TwoWords(String t1, String t2, double connectivity) {
        this.word1 = t1;
        this.word2 = t2;
        this.connectivity = connectivity;


    }

    public int compareTo(Object o) {
        TwoWords that = (TwoWords) o;

        if (that.connectivity > this.connectivity) {
            return 1;
        } else if (that.connectivity < this.connectivity) {
            return -1;
        } else {
            // same connectivity values; ordered by word value
            assert this.word1 != null;
            if (!this.word1.equals(that.word1)) {
                return this.word1.compareTo(that.word1);
            } else {//
                return this.word2.compareTo(that.word2);
            }
        }
    }

    public double getConnectivity() {
        return this.connectivity;
    }

    public void setConnectivity(double connectivity) {
        this.connectivity = connectivity;
    }

    public String getWord1() {
        return this.word1;
    }

    public String getWord2() {
        return word2;
    }

    // equlaity and hash code depends on word couple as well as connectivity
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TwoWords that = (TwoWords) o;

        if (Double.compare(that.connectivity, connectivity) != 0) return false;
        if (word1 != null ? !word1.equals(that.word1) : that.word1 != null) return false;
        if (word2 != null ? !word2.equals(that.word2) : that.word2 != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        long temp;
        temp = connectivity != +0.0d ? Double.doubleToLongBits(connectivity) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        result = 31 * result + (word1 != null ? word1.hashCode() : 0);
        result = 31 * result + (word2 != null ? word2.hashCode() : 0);
        return result;
    }

    public boolean wordsAreNotConnected(){
        return connectivity == NO_CONNECTIVITY;
    }

    public String toString() {
        return word1 + " <-[" + connectivity + "]-> " + word2;
    }
}